<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="巡查线路" prop="lineId">
        <el-select @visible-change="lineVisibleChange($event)" v-model="queryParams.lineId" placeholder="请选择线路"
                   clearable size="small">
          <el-option v-for="line in lineOptions" :label="line.lineName" :value="line.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="安保小组" prop="groupId">
        <el-select @visible-change="guardVisibleChange($event)" v-model="queryParams.groupId"
                   placeholder="请选择安保小组" clearable size="small">
          <el-option v-for="group in groupOptions" :label="group.groupName" :value="group.groupId"/>
        </el-select>
      </el-form-item>
      <el-form-item label="巡查日期" prop="date">
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="queryParams.date"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择巡查日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="isShowOperate">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['patrol:guard:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleLongTerm"
        >长期排班
        </el-button>
      </el-col>
      <!--	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table v-loading="loading" :data="lineGuardList" @selection-change="handleSelectionChange" style="color: #333333"
              :header-cell-style="{background:'#E3E3E3'}">
      <el-table-column label="学校名称" align="center" prop="line.school.name" v-if="!isShowOperate"/>
      <el-table-column label="学校类别" align="center" prop="line.school.type" :formatter="schoolTypeFormat"
                       v-if="!isShowOperate"/>
      <el-table-column label="巡查日期" align="center" prop="date"/>
      <el-table-column label="线路" align="center" prop="line.lineName"/>
      <el-table-column label="安保小组" align="center" prop="group.groupName">
        <template v-slot="{row}">
          <div>{{ row.group && row.group.groupName }}
            <span v-if="row.group&&row.group.delFlag==='1'" style="color: red;font-size: 12px">(已删除)</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="isShowOperate">
        <template slot-scope="scope">
          <span v-if="scope.row.patrolStatus == 0">
            <el-button
              v-if="scope.row.group?scope.row.group.delFlag!='1':true"
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['patrol:guard:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['patrol:guard:remove']"
            >删除</el-button>
          </span>
          <span v-else>/</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改巡查排班(安保)对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="430px" append-to-body>
      <el-form class="elForm" ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="线路" prop="lineId">
          <el-select v-model="form.lineId" placeholder="请选择线路" style="width: 300px">
            <el-option v-for="line in lineOptions" :label="line.lineName" :value="line.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="安保小组" prop="groupId">
          <el-select v-model="form.groupId" placeholder="请选择安保小组" style="width: 300px">
            <el-option v-for="group in groupOptions" :label="group.groupName" :value="group.groupId"/>
          </el-select>
        </el-form-item>
        <el-form-item label="巡查日期" prop="dates">
          <el-date-picker clearable size="small" style="width: 300px"
                          v-model="form.dates"
                          :type="type"
                          :picker-options="pickerTime"
                          @input="$forceUpdate()"
                          value-format="yyyy-MM-dd"
                          placeholder="选择巡查日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listLineGuard,
  getLineGuard,
  delLineGuard,
  addLineGuard,
  updateLineGuard,
  exportLineGuard
} from "@/api/patrol/line_guard";
import {listLineAll} from "@/api/patrol/line";
import {listGroupAll} from "@/api/patrol/group";
import {checkPermi} from "@/utils/permission";

export default {
  name: "Guard",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 巡查排班(安保)表格数据
      lineGuardList: [],
      lineOptions: [],
      groupOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        lineId: null,
        groupId: null,
        schoolId: null,
        date: null,
      },
      // 表单参数
      form: {},
      pickerTime: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7;//如果没有后面的-8.64e7就是不可以选择今天的;
        }
      },
      rules: {
        lineId: [
          {required: true, message: "线路不能为空", trigger: "change"}
        ],
        groupId: [
          {required: true, message: "安保小组不能为空", trigger: "change"}
        ],
        dates: [
          {required: true, message: "巡查日期不能为空", trigger: "change"}
        ],
      },
      type: "dates",
    };
  },
  computed: {
    isShowOperate: function () {
      return checkPermi(['patrol:guard:edit']);
    },
  },
  created() {
    let schoolInfo = this.$route.query.schoolInfo;
    if (schoolInfo) {
      this.queryParams.schoolId = schoolInfo.id;
    }
    this.getSchoolTypeOptions();
    this.getLineOptions();
    this.getGroupOptions();
    this.getList();
  },
  methods: {
    chooseChange() {
      if (this.type === "dates") {
        this.form.dates = []
      }
    },
    // 巡查路线下拉触发
    lineVisibleChange(e) {
      if (e) {
        this.getLineOptions();
      }
    },
    // 安保小组下拉触发
    guardVisibleChange(e) {
      if (e) {
        this.getGroupOptions();
      }
    },
    getSchoolTypeOptions() {
      this.loading = true;
      this.getDicts("shool_type").then(response => {
        this.schoolTypeOptions = response.data;
        this.loading = false;
      });
    },
    schoolTypeFormat(row, column) {
      return this.selectDictLabel(this.schoolTypeOptions, row.line.school.type);
    },
    /** 查询巡查排班(安保)列表 */
    getList() {
      this.loading = true;
      listLineGuard(this.queryParams).then(response => {
        this.lineGuardList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getLineOptions() {
      this.loading = true;
      listLineAll({"lineType": 0, "schoolId": this.queryParams.schoolId}).then(response => {//线路类别：0、安保小组，1、行政人员
        this.lineOptions = response.data;
        this.loading = false;
      })
    },
    getGroupOptions() {
      this.loading = true;
      listGroupAll({groupType: 1, "groupSchoolId": this.queryParams.schoolId}).then(response => {
        this.groupOptions = response.data;
        this.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        lineId: null,
        groupId: null,
        date: null,
        dates: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
      this.getLineOptions();
      this.getGroupOptions();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.type = "dates";
      this.open = true;
      this.title = "添加巡查排班(安保)";
    },
    /**
     * 长期排班
     */
    handleLongTerm() {
      this.$router.push({
        path: '/district/guardLongTerm'
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.type = "date";
      const id = row.id || this.ids
      getLineGuard(id).then(response => {
        this.form = response.data;
        this.form.dates = response.data.date;
        this.open = true;
        this.title = "修改巡查排班(安保)";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.date = this.form.dates;
            this.form.dates = null;
            updateLineGuard(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLineGuard(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.resetQuery();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除所选数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        type: "warning"
      }).then(function () {
        return delLineGuard(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有巡查排班(安保)数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        type: "warning"
      }).then(function () {
        return exportLineGuard(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
<style scoped>

</style>
